﻿Ext.override(NExt.UI.Browse.Data.DataBrowsePresenter, {

    templateInitializeEvents: function() {
        NExt.UI.Browse.Data.DataBrowsePresenter.superclass.templateInitializeEvents.call(this);
        this.addEvents('beforeNew', 'afterNew');
        this.addEvents('beforeOpen', 'afterOpen');
        this.addEvents('beforeDelete', 'afterDelete');
    },

    fireNew: function() {
        this.fireEvent('beforeNew');
        this.templateNew();
        this.fireEvent('afterNew');
    },

    templateNew: function() {
        var editPresenter = this.getEditPresenter();
        var model = this.getNewModel();
        var editingType = NExt.edit.presenter.EditingType.Insert;

        editPresenter.showPresenter(model, editingType);
    },

    fireOpen: function() {
        this.fireEvent('beforeOpen');
        this.templateOpen();
        this.fireEvent('afterOpen');
    },

    templateOpen: function() {
        var editPresenter = this.getEditPresenter();
        var model = this.getCurrentModel();
        var editingType = NExt.edit.presenter.EditingType.Open;

        editPresenter.showPresenter(model, editingType);
    },

    fireDelete: function() {
        this.fireEvent('beforeDelete');
        this.templateDelete();
        this.fireEvent('afterDelete');
    },

    templateDelete: Ext.emptyFn,

    getNewModel: function() { return {}; },

    getCurrentModel: Ext.emptyFn,

    getEditPresenter: Ext.emptyFn,

    initializeRights: function() {
        var isNewEnabled = this.rightSetter.isEnabled(NExt.UI.Base.Right.Action.New);
        isNewEnabled = Ext.isDefined(isNewEnabled) ? isNewEnabled : false;

        var isDeleteEnabled = this.rightSetter.isEnabled(NExt.UI.Base.Right.Action.Delete);
        isDeleteEnabled = Ext.isDefined(isDeleteEnabled) ? isDeleteEnabled : false;

        this.tbbNew.setDisabled(!isNewEnabled);
        this.tbbDelete.setDisabled(!isDeleteEnabled);

        var isOpenEnabled = this.rightSetter.isEnabled(NExt.UI.Base.Right.Action.Open);
        var isEditEnabled = this.rightSetter.isEnabled(NExt.UI.Base.Right.Action.Edit);

        if (Ext.isDefined(isOpenEnabled)) {
            this.tbbOpen.setDisabled(!isOpenEnabled);
        }
        else if (Ext.isDefined(isEditEnabled)) {
            this.tbbOpen.setDisabled(!isEditEnabled);
        }
        else {
            this.tbbOpen.setDisabled(false);
        }
    },

    templateShow: function() {
        NExt.UI.Browse.Data.DataBrowsePresenter.superclass.templateShow.call(this);
        this.initializeRightSetter();
    },

    initializeRightSetter: function() {
        if (this.rightSetter instanceof NExt.UI.Base.Right.CompositeRightSetter) {
            for (i = 0; i < this.rightSetter.rightSetterList.length; i++) {
                var innerRightSetter = this.rightSetter.rightSetterList[i];
                if (innerRightSetter instanceof NExt.UI.Base.Right.BindingSourceRightSetter) {
                    innerRightSetter.setBindingSource(this);
                }
            }
        }

        if (this.rightSetter instanceof NExt.UI.Base.Right.BindingSourceRightSetter) {
            this.rightSetter.setBindingSource(this);
        }

        this.rightSetter.on('needRefresh', this.initializeRights, this);
        this.initializeRights();
    }

});
